/*********************************************************************
 *                                                                   *
 *  Js Css Minifier ( JsCssMin )                                     *
 *  Author: Igor Kanshyn                                             *
 *  Copyright (c) 2009, Igor Kanshyn. All rights reserved.           *
 *                                                                   *
 *  JsCssMin is released under the Apache Software License.          *
 *  See LICENSE.txt for more details.                                *
 *                                                                   *
 *********************************************************************/

package org.grenader.jscssmin;

import org.grenader.jscssmin.model.*;


/**
 * <p>Product: JsCssMin ( JavaScript and CSS Minifier )<br>
 * Author: Igor Kanshyn (grenader).
 * </p>
 * Date: Apr 25, 2009
 */
public class ResourceUtils {

    public static ResourceType getResourceTypeByLine(String line) {
        if (ResourceUtils.isJavaScriptLine(line))
            return ResourceType.JavaScript;
        if (ResourceUtils.isCSSLine(line))
            return ResourceType.CSS;

        throw new IllegalArgumentException("Resource type is undefined. Line [" + line + "] doesn't represent any known resource.");
    }

    static boolean isCSSLine(String line) {
        if (line == null || line.isEmpty())
            return false;

        if (!line.matches("<link.+/>"))
            return false;
        String innerText = line.replaceAll("<link(.+)/>", "$1");

        if (innerText.toLowerCase().indexOf("href=".toLowerCase()) == -1)
            return false;

        if (innerText.toLowerCase().indexOf("rel=\"stylesheet\"".toLowerCase()) == -1 &&
                innerText.toLowerCase().indexOf("rel='stylesheet'".toLowerCase()) == -1 &&
                innerText.toLowerCase().indexOf("rel=stylesheet".toLowerCase()) == -1)
            return false;

        return true;
    }

    static boolean isJavaScriptLine(String line) {
        if (line == null || line.isEmpty())
            return false;

        if (!line.matches("<script.+></script>"))
            return false;
        String innerText = line.replaceAll("<script(.+)></script>", "$1");

        if (innerText.toLowerCase().indexOf("src=".toLowerCase()) == -1)
            return false;

        if (innerText.toLowerCase().indexOf("type=\"text/javascript\"".toLowerCase()) == -1 &&
                innerText.toLowerCase().indexOf("type='text/javascript'".toLowerCase()) == -1 &&
                innerText.toLowerCase().indexOf("type=text/javascript".toLowerCase()) == -1)
            return false;

        return true;
    }

    public static Resource createResource(String line) {
        ResourceType type = getResourceTypeByLine(line);
        if (type == ResourceType.JavaScript)
            return new JavaScriptResource(line);
        else if (type == ResourceType.CSS)
            return new CSSResource(line);
        else
            throw new IllegalArgumentException("Invalid resource type!");

    }

    public static ResourceGroup createResourceGroup(ResourceType type) {
        if (type == ResourceType.JavaScript)
            return new JavaScriptResourceGroup();
        else if (type == ResourceType.CSS)
            return new CSSResourceGroup();
        else
            throw new IllegalArgumentException("Invalid resource type!");
    }
}